package email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Implements email message
 * @author Alexandre, Cristovao, Raul, Rodolpho, Tales
 * @version 1.1
 * @milestone Number 2
 */
public class Email {

	private static Email email = null;
	private Properties properties;
	private Session session;

	// gmail account data (login/password), this account is used to send the
	// e-mails
	private final String LOGIN = "imheresystem@gmail.com";
	private final String PASSWORD = "10im29here";

	/**
	 * Constructor of the class. This implements the connection between the ImHere System and the Gmail account.
	 */
	protected Email() {
		// configures the mail account properties
		this.properties = new Properties();
		properties.put("mail.smtp.host", "smtp.gmail.com");
		properties.put("mail.smtp.auth", "true");
		properties.put("mail.mime.charset", "ISO-8859-1");
		properties.put("mail.smtp.port", "465");
		properties.put("mail.smtp.starttls.enable", "true");
		properties.put("mail.smtp.socketFactory.port", "465");
		properties.put("mail.smtp.socketFactory.fallback", "false");
		properties.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		// login into gmail account
		this.session = Session.getInstance(properties, new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(LOGIN, PASSWORD);
			}
		});

	}

	/**
	 * Singleton's getInstance implementation of the class
	 * @return the Email instance
	 */
	public synchronized static Email getInstance() {
		if (email == null) {
			email = new Email();
		}
		return email;
	}

	/**
	 * This method is a wrapping that send the email
	 * 
	 * @param to
	 *            the destination email
	 * @param subject
	 *            the subject of the email
	 * @param text
	 *            the body/content of the email
	 * @throws MessagingException
	 *             if any parameter is invalid
	 * @see the JavaMail api for details
	 */
	public synchronized void sendEMail(String to, String subject, String text)
			throws MessagingException {
		// create the message
		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress("login"));
		message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
		message.setSentDate(new Date());
		message.setSubject(subject);
		message.setText(text);
		// send the email
		Transport.send(message);
	}

	/**
	 * Send an email to the user.
	 * @param emailMessage - the message of the email
	 * @throws MessagingException if occur any error.
	 */
	public synchronized void sendEMail(EmailMessage emailMessage) throws MessagingException {
		sendEMail(emailMessage.getTo(), emailMessage.getSubject(), emailMessage
				.getMessage());
	}
}
